package com.liang.blog.system.controller;

import com.liang.blog.common.exception.BlogException;
import com.liang.blog.common.result.Result;
import com.liang.blog.model.BlogDict;
import com.liang.blog.system.service.BlogDictService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2023-08-30-19:33
 */
@RestController
@RequestMapping("/dict")
public class BlogDictController {

    @Autowired
    private BlogDictService blogDictService;

    /**
     * 根据 dictCode 获取下级节点
     * @param code
     * @return
     */
    @GetMapping("getBlogDictByCode/{code}")
    public Result getDictByCode(@PathVariable String code){
        List<BlogDict> blogDictByCode = blogDictService.getBlogDictByCode(code);
        return Result.ok(blogDictByCode);
    }


    @ApiOperation("根据数据 id 查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable String id) {
//        List<Dict> dictInfo = (List<Dict>)redisTemplate.boundHashOps("List").get("Dict" + id);
//        if (dictInfo == null) {
        List<BlogDict> list = blogDictService.findChildData(Long.parseLong(id));
        return Result.ok(list);
//        }
//        return Result.ok(dictInfo);
    }

    @ApiOperation("导出数据字典接口")
    @GetMapping("export")
    public Result exportDict(HttpServletResponse response) {
        blogDictService.exportDict(response);
        return Result.ok();
    }

    @ApiOperation("导入数据字典接口")
    @PostMapping("import")
    public Result importData(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
        if ("xlsx".equals(extension) || "xls".equals(extension)) {
            blogDictService.importData(file);
            return Result.ok();
        }
        throw new BlogException("文件格式错误！", 444);
    }

    @ApiOperation("根据 dictcode 和 value 查询")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value) {
        return blogDictService.getDictName(dictCode, value);
    }

    @ApiOperation("根据 value 查询")
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        return blogDictService.getDictName("", value);
    }

//    @ApiOperation("根据 dictCode 获取下级节点")
//    @GetMapping("findByDictCode/{dictCode}")
//    public Result findByDictCode(@PathVariable String dictCode) {
//        List<BlogDict> list = blogDictService.findByDictCode(dictCode);
//        return Result.ok(list);
//    }

//    @GetMapping("getAll/{id}")
//    public Result getAll(@PathVariable String id){
//        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
//        dictQueryWrapper.eq("parent_id", id);
//        List<Dict> all = dictService.getAll(dictQueryWrapper);
//        return Result.ok(all);
//    }


}
